package de.nak.fireDepartment.administrationAction;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.struts.ActionSupport;

import de.nak.fireDepartment.actionForms.EquipmentForm;
import de.nak.fireDepartment.model.Equipment;
import de.nak.fireDepartment.service.EquipmentService;
import de.nak.fireDepartment.service.LoginService;

/**
 * Action that shows the list of equipments
 * 
 * @author Nils Bredow
 */
public class ShowEquipmentListAction extends ActionSupport {

	/**
	 * This method list all equipments
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// Get the service
		WebApplicationContext ctx = getWebApplicationContext();
		EquipmentService equipmentService = (EquipmentService) ctx.getBean("equipmentService");
		
		// Validate the user-login
		LoginService loginService = (LoginService)getWebApplicationContext().getBean("loginService");
		if(!loginService.checkUserIsValidated()){
			return mapping.findForward("missinglogin");
		}
		
		// get all equipments
		Collection<Equipment> equipments = equipmentService.listEquipments();
		// Store value in form
		EquipmentForm equipmentListForm = (EquipmentForm) form;
		equipmentListForm.setEquipments(equipments);
		equipmentListForm.setErrorCode("");
		// Return result
		return mapping.findForward("success");
	}
}
